Component({
  data: {
    color: "",
    selectedColor: "#1684FC",
    list: [{
      pagePath: "/pages/index/index",
      iconPath: "/imgs/tabar/首页.png",
      selectedIconPath: "/imgs/tabar/首页 (1).png",
      text: "首页",
      index: "0"
    }, {
      pagePath: "/pages/venue/venue",
      iconPath: "/imgs/tabar/场馆-01.png",
      selectedIconPath: "/imgs/tabar/场馆-01 (1).png",
      text: "场馆",
      index: "1"
    },{
      pagePath: "/pages/invitation/invitation",
      iconPath: "/imgs/tabar/帖子.png",
      selectedIconPath: "/imgs/tabar/帖子 (1).png",
      text: "热门帖",
      index: "2"
    },{
      pagePath: "/pages/myHome/myHome",
      iconPath: "/imgs/tabar/我的.png",
      selectedIconPath: "/imgs/tabar/我的 (1).png",
      text: "我的",
      index: "3"
    }]
  },
  // lifetimes:进入页面之前执行的函数,生命周期
  // attached()生命周期函数,经常用于在组件加载前进初始化操作
  lifetimes: {
    attached() {
      // wx.getStorageSync同步获取本地缓存的方法
      let selected = wx.getStorageSync('selected');
      console.log(selected);
      if(selected === null || selected === undefined || selected === "") {
        selected = 0;
        wx.setStorageSync('selected', selected);
      }
      console.log(selected+"缓存数据");
      this.setData({selected});
    }
  },
  methods: {
    switchTab(e) {
      // e是事件对象,获取用户点击事件的索引
      const index = e.currentTarget.dataset.index;
      const url = this.data.list[index].pagePath;
      this.setData({
        selected: index
      });
      wx.setStorageSync('selected', index);
      // wx.switchTab微信跳转方法,每次会销毁之前的页面,无法返回
      // wx.navigateTo不会销毁之前的界面,可以返回
      wx.switchTab({
        url,
        success: () => {
          console.log(url+"路径");
          console.log(index + "索引");
        }
      });
    }
  }
})